package 内地居民结婚登记数占比;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MarriageRatioMapper extends Mapper<LongWritable, Text, Text, MarriageRatioBean> {
    private Text outKey = new Text();
    private MarriageRatioBean outValue = new MarriageRatioBean();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 按照逗号分割字符串，获取各个字段
        //省级,市级,年份,结婚登记(万对),内地居民登记结婚(万对),内地居民初婚登记(万对),内地居民再婚登记(万对),涉外及港澳台居民登记结婚(万对),离婚登记(万对),人口总数（万人）,粗离婚率(‰)
        String[] fields = value.toString().split(",");
        if (fields.length == 10) {
            String year = fields[2];
            float marriageCount = Float.parseFloat(fields[4]);
            float firstmarriageCount = Float.parseFloat(fields[5]);
            float remarriageCount = Float.parseFloat(fields[6]);
            // 设置输出的键为年份
            outKey.set(year);
            // 创建MarriageRatioBean对象并设置相关属性
            outValue.set(marriageCount,firstmarriageCount,remarriageCount);

            // 将键值对写入上下文，传递给Reducer
            context.write(outKey, outValue);
        }
    }
}